<template>
  <div class="app-container">

    <el-form :model="form" ref="form" label-width="160px" v-loading="formLoading" :rules="rules">
      <el-form-item label="试题上传：" required>
        <el-upload
          class="upload-demo"
          action="http://175.24.33.88:8000/api/admin/excel/import_excel_for_question"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          ref='upload'
          :file-list='fList'
          multiple
          :auto-upload="true"
          :http-request="cfileUpload"
          :with-credentials="true"
        >
          <el-button size="small" icon="el-icon-plus" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传.xlx/.xlsx文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>

    </el-form>


  </div>
</template>

<script>
import { allCategory,selectCategory,getCategoryPageList,createCategory,deleteCategory,changeStatus} from "@/api/category/category";
import Editor from '@/components/Editor';
import axios from 'axios'
import {form} from "@/utils/request"
import request from '@/utils/request'
export default {
  inject:['reload'],
   components: {
    Editor
  },
  data() {
    return {
      fList:null,
      form: {

      },
      formLoading: false,
      rules: {
         concreteName: [
          { required: true, message: "请输入课程类别名称", trigger: "blur" }
        ]
      }
    }
  },
  created() {
   let _this = this
  },
  methods: {
     cfileUpload(file){
    this.$refs.form.validate((valid) => {
        if (valid) {
          this.formLoading = true
          let files = file.file
          //console.log(file.file)
          let formObj = new FormData();
          //formObj.append("classId", this.form.classId)

          //console.log(classId);
          formObj.append("uploadExcel", files)
        request({
        method: "post",
        url: "/api/admin/excel/import_excel_for_question", // 请求地址
        data: formObj, // 参数
        //responseType: "blob", // 表明返回服务器返回的数据类型
        headers: {
          "Content-Type": "application/json"
        }
      }).then(res => {
         // form("/api/admin/excel/import_excel_for_question",formObj).then(res => {
            //console.log(res);
            if (res.code === 1) {

                    this.$message.success(res.message)
                    this.$router.push('/exam/question/index')
                }
            else {
                      this.$message({
                        showClose: true,
                        message: "上传失败!"
                      });
                      this.$refs.upload.clearFiles()
                      request({
                      method: "post",
                      url: "/api/admin/excel/import_excel_for_question", // 请求地址
                      data:formObj, // 参数
                      responseType: "blob", // 表明返回服务器返回的数据类型
                      headers: {
                        "Content-Type": "application/json"
                      }
                    }).then(res => { // 处理返回的文件流
                      const blob = new Blob([res]);
                      const fileName = "试题添加error.xlsx";
                      const elink = document.createElement("a");
                      elink.download = fileName;
                      elink.style.display = "none";
                      elink.href = URL.createObjectURL(blob);
                      document.body.appendChild(elink);
                      elink.click();
                      URL.revokeObjectURL(elink.href); // 释放URL 对象
                      document.body.removeChild(elink);
                    })
                    this.formLoading = false
                }
        }).catch(e => {
            this.formLoading = false
          })
        }
        else {
          this.$refs.upload.clearFiles()
          return false
        }
      })
    },
    resetForm () {
      this.reload()
    }
  }
};
</script>
